home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-08-01 | 8.5 KB | 343 lines |
- // purpose: creates a Java source file for PalmOS which would create
- // a database containing the game's levels. The sources file
- // contains the levels, built from .bmp files
-
- import java.io.*;
- import java.util.*;
-
- import java.awt.*;
- import java.awt.image.*;
-
- public class Builder
- {
- private static final int NAME_SPACE = 12;
- private static final String TEMPLATE = "LevelsMaker.template";
- private static final String TEMPLATE_MATCH = "%TEMPLATE%";
-
- private final static Component component = new Component() { };
- private final static MediaTracker tracker = new MediaTracker(component);
-
- private int width, height;
- private ImageObserver imageObserver;
-
- Builder()
- {
- build();
- }
-
- public static void main(String[] args)
- {
- new Builder();
- }
-
- private void build()
- {
- try
- {
- // reads template file
- File templateFile = new File(TEMPLATE);
- StringBuffer template = new StringBuffer((int) templateFile.length());
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(templateFile)));
-
- String line;
- int pos = 0, index = -1;
- boolean templateFound = false;
-
- for ( ; (line = in.readLine()) != null; )
- {
- if ((index = line.indexOf(TEMPLATE_MATCH)) != -1)
- {
- pos += index;
- templateFound = true;
- line = line.substring(0, index) + line.substring(index + TEMPLATE_MATCH.length());
- } else if (!templateFound) {
- pos += (line.length() + 1);
- }
-
- template.append(line).append('\n');
- }
-
- in.close();
-
- StringBuffer insert = new StringBuffer();
- String files[] = getWildCardMatches(getUserDirectory() + File.separator + "datas", "*.gif");
- sortStrings(files);
-
- for (int i = 0; i < files.length; i++)
- {
- Image image = Toolkit.getDefaultToolkit().getImage("datas" + File.separator + files[i]);
- loadImage(image);
- int[] bits = addTitle(files[i], getPixels(image));
-
- insert.append("\n {\n ");
- insert.append(" 0, 1,\n ");
-
- for (int j = 0; j < NAME_SPACE; j++)
- insert.append(' ').append(bits[j] == -16777216 ? -1 : bits[j]).append(',');
- insert.append("\n ");
-
- for (int j = 0; j < height; j++)
- {
- for (int k = 0; k < width; k++)
- {
- insert.append(' ').append(handleSinglePixel(bits[(NAME_SPACE - 1) + j * width + k]));
- if ((NAME_SPACE - 1) + j * width + k != bits.length - 1)
- insert.append(',');
- }
-
- if (j != height - 1)
- insert.append("\n ");
- }
-
- insert.append("\n }");
- if (i != files.length - 1)
- insert.append(',');
- }
-
- template.insert(pos, insert.toString());
-
- String outName = TEMPLATE.substring(0, TEMPLATE.indexOf(".template")) + ".java";
- PrintWriter out = new PrintWriter(new FileOutputStream(outName));
- out.print(template.toString());
- out.close();
-
- } catch (IOException ioe) { }
-
- System.exit(0);
- }
-
- private int[] addTitle(String title, int[] bits)
- {
- int[] _bits = new int[bits.length + NAME_SPACE];
-
- int pos = 0;
- for (int i = 0; i < title.length(); i++)
- {
- if (Character.isLetter(title.charAt(i)))
- {
- pos = i;
- break;
- }
- }
-
- title = title.substring(pos, title.length() - 4);
-
- for (int i = 0; i < NAME_SPACE; i++)
- _bits[i] = (i < title.length()) ? (int) title.charAt(i) : -1;
-
- System.arraycopy(bits, 0, _bits, NAME_SPACE - 1, bits.length);
-
- return _bits;
- }
-
- private int handleSinglePixel(int pixel)
- {
- int alpha = (pixel >> 24) & 0xff;
- int red = (pixel >> 16) & 0xff;
- int green = (pixel >> 8) & 0xff;
- int blue = (pixel ) & 0xff;
-
- return (red == 255 && green == 255 && blue == 255) ? 0 : 1;
- }
-
- private void loadImage(Image image)
- {
- synchronized(tracker)
- {
- tracker.addImage(image, 0);
- try
- {
- tracker.waitForID(0, 0);
- } catch (InterruptedException e) {
- System.out.println("INTERRUPTED while loading Image");
- }
- tracker.statusID(0, false);
- tracker.removeImage(image, 0);
-
- width = image.getWidth(imageObserver);
- height = image.getHeight(imageObserver);
- }
- }
-
- /**
- * Returns an array of pixels representing an image.
- * @param image The image to get pixels from
- */
-
- private int[] getPixels(Image image)
- {
- int[] pixels = new int[width * height];
-
- if (image != null)
- {
- try
- {
- PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
- pg.grabPixels();
- if ((pg.getStatus() & ImageObserver.ABORT) != 0)
- {
- System.out.println("#### ERROR");
- }
- } catch (InterruptedException ie) { }
- }
-
- return pixels;
- }
-
- /**
- * Returns user directory.
- */
-
- public static String getUserDirectory()
- {
- return System.getProperty("user.dir");
- }
-
- /**
- * When the user has to specify file names, he can use wildcards (*, ?). This methods
- * handles the usage of these wildcards.
- * @param path The path were to search
- * @param s Wilcards
- * @return An array of String which contains all files matching <code>s</code>
- * in current directory.
- */
-
- public static String[] getWildCardMatches(String path, String s)
- {
- if (s == null)
- return null;
-
- String files[];
- String filesThatMatch[];
- String args = new String(s.trim());
- Vector filesThatMatchVector = new Vector();
-
- if (path == null)
- path = getUserDirectory();
-
- files = (new File(path)).list();
- if (files == null)
- return null;
-
- for (int i = 0; i < files.length; i++)
- {
- if (match(args, files[i]))
- {
- File temp = new File(getUserDirectory(), files[i]);
- filesThatMatchVector.addElement(new String(temp.getName()));
- }
- }
-
- filesThatMatch = new String[filesThatMatchVector.size()];
- filesThatMatchVector.copyInto(filesThatMatch);
-
- return filesThatMatch;
- }
-
- /**
- * This method can determine if a String matches a pattern of wildcards
- * @param pattern The pattern used for comparison
- * @param string The String to be checked
- * @return true if <code>string</code> matches <code>pattern</code>
- */
-
- public static boolean match(String pattern, String string)
- {
- for (int p = 0; ; p++)
- {
- for (int s = 0; ; p++, s++)
- {
- boolean sEnd = (s >= string.length());
- boolean pEnd = (p >= pattern.length() || pattern.charAt(p) == '|');
- if (sEnd && pEnd)
- return true;
- if (sEnd || pEnd)
- break;
- if (pattern.charAt(p) == '?')
- continue;
- if (pattern.charAt(p) == '*')
- {
- int i;
- p++;
- for (i = string.length(); i >= s; --i)
- if (match(pattern.substring(p), string.substring(i))) return true;
- break;
- }
- if (pattern.charAt(p) != string.charAt(s))
- break;
- }
- p = pattern.indexOf('|', p);
- if (p == -1)
- return false;
- }
- }
-
- /**
- * Quick sort an array of Strings.
- * @param string Strings to be sorted
- */
-
- public static void sortStrings(String[] strings)
- {
- sortStrings(strings, 0, strings.length - 1);
- }
-
- /**
- * Quick sort an array of Strings.
- * @param a Strings to be sorted
- * @param lo0 Lower bound
- * @param hi0 Higher bound
- */
-
- public static void sortStrings(String a[], int lo0, int hi0)
- {
- int lo = lo0;
- int hi = hi0;
- String mid;
-
- if (hi0 > lo0)
- {
- mid = a[(lo0 + hi0) / 2];
-
- while (lo <= hi)
- {
- while (lo < hi0 && a[lo].compareTo(mid) < 0)
- ++lo;
-
- while (hi > lo0 && a[hi].compareTo(mid) > 0)
- --hi;
-
- if (lo <= hi)
- {
- swap(a, lo, hi);
- ++lo;
- --hi;
- }
- }
-
- if (lo0 < hi)
- sortStrings(a, lo0, hi);
-
- if (lo < hi0)
- sortStrings(a, lo, hi0);
- }
- }
-
- /**
- * Swaps two Strings.
- * @param a The array to be swapped
- * @param i First String index
- * @param j Second String index
- */
-
- public static void swap(String a[], int i, int j)
- {
- String T;
- T = a[i];
- a[i] = a[j];
- a[j] = T;
- }
- }
-
- // End of Builder.java
-